import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'dateFormat'
})

export class DateFormatPipe implements PipeTransform {
  transform(value: string, type: string): any {
    if(type === 'YMD'){ // 年月日字符串
      return `${value.substring(0, 4)}-${value.substring(4, 6)}-${value.substring(6, 8)} ${value.substring(8, 10)}:${value.substring(10, 12)}`
    }else if(type === 'hh:mm'){ // 预约排队的时间段处理
      return value.substring(0, 2) + ':' + value.substring(2)
    }else{
      const d = new Date(value)
      if(type === 'year'){
        return d.getFullYear()
      }
      if(type === 'month-date'){
        return `${(d.getMonth()+1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`
      }
      if(type === 'month'){
        return `${(d.getMonth()+1).toString().padStart(2, '0')}`
      }
      if(type === 'date'){
        return `${(d.getDate()).toString().padStart(2, '0')}`
      }
      if(type === 'format'){
        return `${d.getFullYear()}-${(d.getMonth()+1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`
      }
      if(type === 'formatTime'){
        return `${d.getFullYear()}-${(d.getMonth()+1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')} ${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}:${d.getSeconds().toString().padStart(2, '0')}`
      }
    }
    
    return value
  }
}